package com.zkthink.ceres.applet.feedback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.common.domin.PageDate;
import com.zkthink.ceres.applet.feedback.domain.vo.AddAppletFeedBackVo;
import com.zkthink.ceres.applet.feedback.domain.vo.AppletFeedbackVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkthink.ceres.applet.feedback.domain.entity.AppletFeedback;
import com.zkthink.ceres.applet.feedback.mapper.AppletFeedbackMapper;
import com.zkthink.ceres.applet.feedback.service.AppletFeedbackService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.zkthink.ceres.applet.authority.config.AuthorityWebConfiguration.getMid;

/**

@Author cmz
@Date 2024-05-07 10:25
*/
@Service
public class AppletFeedbackServiceImpl extends ServiceImpl<AppletFeedbackMapper, AppletFeedback> implements AppletFeedbackService{

    @Resource
    private AppletFeedbackMapper appletFeedbackMapper;
    @Override
    public R insert(AddAppletFeedBackVo addAppletFeedBackVo) {
        AppletFeedback appletFeedback = new AppletFeedback();
        appletFeedback.setMid(getMid());
        appletFeedback.setFeedbackTitle(addAppletFeedBackVo.getFeedbackTitle());
        appletFeedback.setFeedbackContent(addAppletFeedBackVo.getFeedbackContent());
        String join = String.join(",", addAppletFeedBackVo.getFeedbackImg());
        appletFeedback.setFeedbackImg(join);
        appletFeedback.setPhone(addAppletFeedBackVo.getPhone());
        appletFeedback.setCreateTime(new Date());
        int insert = appletFeedbackMapper.insert(appletFeedback);
        return insert>0?R.success():R.fail("添加失败");
    }

    @Override
    public R findFeedback(PageDate pageDate) {
        Page<AppletFeedbackVo> page = new Page<>(pageDate.getPageNum(), pageDate.getPageSize());
        /*QueryWrapper<AppletFeedbackVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("feedback_title",pageDate.getKeyword()).or().like("feedback_content",pageDate.getKeyword());
        queryWrapper.orderByDesc("create_time");*/
        Long mid = getMid();
        Page<AppletFeedbackVo> appletFeedbackPage = appletFeedbackMapper.selectByPage(page,mid);
        List<AppletFeedbackVo> records = appletFeedbackPage.getRecords();
        for (AppletFeedbackVo record : records) {
            String[] split = record.getFeedbackImg().split(",");
            record.setFeedbackImgList(split);
        }
        return R.success(appletFeedbackPage);
    }
}
